
// Classe N4dChatSession

function N4dChatSession(){
   this.alias = null;
}

// Methods

N4dChatSession.prototype.login = function(alias, server){
    // n4d connect
    self=this;
    this.alias=alias;
    this.intervalUsers=null;
    this.intervalMsgs=null;
    this.lastMsg=0;
    
    $.xmlrpc({
        url: 'https://'+server+':9779',
			methodName: 'registerUser',
                        params: ["", "N4dChat", "", alias],
			success: function(response,status,jqXHR){
				if (response==0) {
                                 self.startSession();  
                                } else {
                                    $("#msg_err").empty();
                                    $("#msg_err").html("Register error");
                                
                                }
			},
			error: function(jqXHR, status, error) {
				//alert("Status: "+status+"\nError: N4d server is down"+error);
                                $("#msg_err").empty();
                                $("#msg_err").html("N4d Disconnected");
			}
		})
	
    }


N4dChatSession.prototype.refreshUsers= function(){
   $.xmlrpc({
        url: 'https://'+server+':9779',
			methodName: 'getUsers',
                        params: ["", "N4dChat"],
			success: function(response,status,jqXHR){
                                $("#users").empty();
                                for (i in response[0]) {
                                     $("#users").append("<div class='user'>"+response[0][i]["username"]+":"+response[0][i]["ip"]+"</div>");
                                }
			},
			error: function(jqXHR, status, error) {
				alert("Status: "+status+"\nError: "+error);
			}
		})
}

N4dChatSession.prototype.refreshMessages= function(){
      $.xmlrpc({
        url: 'https://'+server+':9779',
			methodName: 'getMessages',
                        params: ["", "N4dChat", self.lastMsg],
			success: function(response,status,jqXHR){
                                self.lastMsg=response[0]["last_msg"];
				for (i in response[0]["messages"]) {
                                     $("#messages").append("<div class='message'><strong>"+response[0]["messages"][i]["username"]+":</strong>"+response[0]["messages"][i]["message"]+"</div>");
                                }
			},
			error: function(jqXHR, status, error) {
				alert("Status: "+status+"\nError: "+error);
                                
			}
		})
}


N4dChatSession.prototype.sendMessage= function(msg){
      console.log("sending "+msg);
      $.xmlrpc({
        url: 'https://'+server+':9779',
			methodName: 'sendMessage',
                        params: ["", "N4dChat", "", self.alias, msg],
			success: function(response,status,jqXHR){
                                self.refreshMessages();
			},
			error: function(jqXHR, status, error) {
				alert("Status: "+status+"\nError: "+error);
                                
			}
		})
}

N4dChatSession.prototype.startSession = function(){
   // Resize Window
   var gui = require('nw.gui');
   var win = gui.Window.get();
   win.resizeTo(640, 480);
   
   // Hide login and show chat
   $("#loginWindow").hide();
   $("#chatWindow").show();
   
   // Set timers
   self.refreshUsers();
   //self.refreshMessages(0);
   
   self.intervalUsers=setInterval(self.refreshUsers, 10000);
   self.intervalMsgs=setInterval(function () {self.refreshMessages()}, 3000);

}

$(document).ready(function() {
    var chatSession=new N4dChatSession();
    // Bind Event Handlers
    
  $("#LoginButton").bind('click', function( event ){
	username=$("#input_username").val();
	server=$("#input_server").val();                        
        chatSession.login(username,server);
    });
  
  $("#sendMsg").bind('click', function( event ){
	msg=$("#msg").val();
        if (msg!="") chatSession.sendMessage(msg);
    });  
    
});





